<?php
/* @var $this DemandeController */
/* @var $model Demande */
/* @var $form CActiveForm */
$cs = Yii::app()->getClientScript();
$cs->registerScript('form_grains', "
	function check_selistes(idgrain) {
		$.ajax({
			url: '" . Yii::app()->createAbsoluteUrl("seliste/getSelistesGrain") . "',
			type: 'get',
        	datatype: 'json',
			data: {
				id: idgrain,
			},
			success:function(data) {
				var toAppend = '<option value=\'0\'>Tous les sélistes</option>';
           		var selistes = JSON.parse(data);
				$.each(selistes, function (k,v) {
					toAppend += '<option value=\''+v.id+'\'>'+v.seliste+'</option>';
				});
				$('#Demande_idseliste_cible').empty().append(toAppend);
			},
		});
	}
	function remove_selistes() {
        $('#Demande_idseliste_cible').empty().append('<option value=\'0\'>Tous les sélistes</option>');
	}
	$('form input[type=radio], form input[type=checkbox]').change(function() {
		var choice = $(this).attr('id');
		$('input.checkgrain:checked').each(function(index) {
			if ($(this).attr('id') != choice) {
				$(this).attr('checked', false);
				$(this).parent().removeClass('active');
				$(this).next('.checkOk').remove();
			}
		});
		addOkCheck();
		if ($(this).hasClass('checkgrain')) {
			if($(this).is(\":checked\")) {
				$('#choix-grain').html('<h4><span id=\"label-grain\" data-id=\"'+$(this).attr('data-grain')+'\" class=\"label label-info\">'+$.trim($('#liste_grains div.active ol li')[0].innerText)+' / '+$.trim($('#liste_grains div.active ol li')[1].innerText)+' / '+$(this).next().next().text()+'</span></h4>');
				$('#panel-choix').slideDown();
				check_selistes($(this).attr('data-grain'));
			} else {
				$('#panel-choix').slideUp();
				$('#choix-grain').html('');
			}
		}
	});
	$('#btn-validegrain').click(function() {
		$('#Demande_idgrain').val($('#label-grain').attr('data-id'));
		$('#choix_grain').slideUp();
		$('#ajout_grain').slideUp();
		$('#label-grain').removeClass('label-info').addClass('label-success');
		$('#btn-removegrain').show();
		$(this).hide();
		$('#demande_etape_deux').slideDown();
		$('#btn-validedemande').show();
	});
	$('#btn-removegrain').click(function() {
		$('#Demande_idgrain').val(0);
		$('input.checkgrain:checked').each(function(index) {
			$(this).attr('checked', false);
			$(this).parent().removeClass('active');
			$(this).next('.checkOk').remove();
		});
		remove_selistes();
		$('#choix-grain').html('');
		$('#choix_grain').slideDown();
		$('#ajout_grain').slideDown();
		$('#btn-validegrain').show();
		$('#panel-choix').slideUp();
		$(this).hide();
		$('#demande_etape_deux').slideUp();
		$('#btn-validedemande').hide();
	});
	$('#btn-ajoutgrain').click(function() {
		$('#choix_grain').slideUp();
		$('#form_ajout_grain').slideDown();
		$(this).hide();
	});
	$('#btn-addgrain').click(function() {
		if ($('#Grain_nom').val() != '') {
			remove_selistes();
			$('#form-group-nom').removeClass('has-error');
			$('#form_ajout_grain').slideUp();
			$('#choix-grain').html('<h4><span id=\"label-grain\" data-id=\"0\" class=\"label label-info\">'+$.trim($('#Grain_idtype option:selected').text())+' / '+$.trim($('#Grain_iddomaine option:selected').text())+' / '+$.trim($('#Grain_nom').val())+'</h4>');
			$('#panel-choix').slideDown();
			$('#choix_grain').slideDown();
			$('#btn-ajoutgrain').show();
		} else {
			$('#form-group-nom').addClass('has-error');
		}
		
	});
	$('#btn-annulegrain').click(function() {
		$('#choix_grain').slideDown();
		$('#form_ajout_grain').slideUp();
		$('#btn-ajoutgrain').show();
	});
	$('.unvalidate').tooltip();
",CClientScript::POS_READY );
$cs->registerScript('form_validate', "
	function valide_form(form) {
		var testArr = ['0','1','2','3'];
		if ($.inArray($('input[name=\"Demande[modecontact]\"]:checked').val(),testArr) == -1) {
			$('#form-group-mode').addClass('has-error');
			return false;
		} else {
			$('#form-group-mode').removeClass('has-error');
			return true;
		}
	}
",CClientScript::POS_HEAD );
?>

<div class="form">

<?php 
$form=$this->beginWidget('CActiveForm', array(
		'id'=>'demande-form',
		'htmlOptions'=>array('class'=>'form-horizontal','onSubmit'=>'return valide_form(this)'),
		'enableAjaxValidation'=>false,
));
?>

    <?php $labels = $model->attributeLabels();?>

	<p class="note">
		Les champs marqués d'une <span class="required">*</span> sont requis.
	</p>

	<?php echo $form->errorSummary($model,NULL,NULL,array("class"=>"alert alert-danger")); ?>
	
	<?php echo $form->hiddenField($model,'idseliste_saisie',array("value"=>Yii::app()->user->id)); ?>
	<?php echo $form->hiddenField($model,'status',array("value"=>1)); ?>
	<?php 
	if (!Yii::app()->user->admin) {
		echo $form->hiddenField($model,'idseliste',array("value"=>Yii::app()->user->id)); 
	} else {
		$model->idseliste = Yii::app()->user->id;
		?>
		<div class="form-group">
             	<?php echo $form->labelEx($model,'idseliste',array("class"=>"col-lg-3 control-label")); ?>
                <div class="col-lg-9">
                        <?php 
                        $data = CHtml::listData(Seliste::model()->findAll(array('order'=>'nom asc')),
                                'id', 
                                function($model) {
                                        $retour = $model->id;
										$retour .= " (";
										$retour .= $model->pseudo;
										$retour .= " - ";
										$retour .= $model->nom . " ";
										$retour .= $model->prenom;
										$retour .= ")";
										if ($model->id == Yii::app()->user->id) $retour = "Moi-même";
										return $retour;
                                }
                        );
                        echo $form->dropDownList($model,'idseliste',$data,array("class"=>"form-control"));
                        ?>
                </div>
		</div>
	<?php } ?>
	
	<div class="form-group<?php if ($model->getErrors('igrain')) echo " has-error";?>">
		<?php echo $form->labelEx($model,'idgrain',array("class"=>"col-lg-3 control-label")); ?>
		<?php echo $form->hiddenField($model,'idgrain',array("value"=>0)); ?>
		<div class="col-lg-9">
			
			<div class="panel panel-default" id="panel-choix">
				<div class="panel-body">
				    Grain sélectionné : <span id="choix-grain"></span>
				    <?php
					echo CHtml::htmlButton ( "Valider ce choix", array (
							'id' => "btn-validegrain",
							'class' => "btn btn-primary" 
					));
					echo CHtml::htmlButton ( "<span class=\"glyphicon glyphicon-remove\"></span>Annuler ce choix", array (
							'id' => "btn-removegrain",
							'class' => "btn btn-danger"
					));
					?>
			  	</div>
			</div>
		
			<div id="choix_grain">
				<h4>Sélectionnez un grain existant :</h4>
				<?php // navbar type/domaine ?>
				<ul class="nav nav-tabs" id="myTab">
				<?php
				$types = Type::model()->findAll(array('order'=>'lib asc'));
				$domaines = Domaine::model()->findAll(array('order'=>'lib asc'));
				$i=0;
				foreach ($types as $type) {
					?>
					<li class="dropdown<?php echo (($i==0) ? " active" : "");?>"><a
						class="dropdown-toggle" data-toggle="dropdown" href="#">
							<?php echo $type->lib;?> <span class="caret"></span>
					</a>
						<ul class="dropdown-menu">
						<?php
						foreach ($domaines as $domaine) {
							// on regarde s'il y a des grains dans ce type/domaine
							$grains = Grain::model()->findAll("idtype=" . $type->id . " AND iddomaine=" . $domaine->id,array("order"=>"nom asc"));
							$g=0;
							foreach ($grains as $grain) {
								// on regarde si l'utilisateur courant a le droit de visualiser le grain
								if ($grain->status==1 || $grain->idseliste==Yii::app()->user->id || Yii::app()->user->admin_grain) $g++;
							}
							$ast = "";
							if ($g>0) $ast = "<span class=\"badge pull-right\">" . $g . "</span>";
							$i++;
							?>
							<li <?php echo (($i==1) ? " class=\"active\"" : "");?>>
								<a class="atab"	href="#onglet<?php echo $type->id . $domaine->id;?>" data-toggle="tab"><?php echo $ast . $domaine->lib;?></a></li>
							<?php
						}
						?>
						</ul></li>
					<?php
				}
				?>
				</ul>
			
				<?php // onglets type/domaine ?>
				<div class="tab-content panel panel-default" id="liste_grains">
				<?php
				$i=0;
				foreach ($types as $type) {
					foreach ($domaines as $domaine) {
						$i++;
						echo "<div id=\"onglet" . $type->id . $domaine->id . "\" class=\"tab-pane fade" . (($i==1) ? " in active" : "") . "\">";
						?>
							<ol class="breadcrumb">
								<li><?php echo $type->lib;?></li>
								<li><?php echo $domaine->lib;?></li>
							</ol>
	
							<div class="form-group"	id="choix_<?php echo $type->id;?>_<?php echo $domaine->id;?>">
								<?php	$this->renderPartial('_tab_choix_grain',array('domaine'=>$domaine->id,'type'=>$type->id));?>
							</div>
						<?php echo "</div>"; ?>
						
					<?php
					
					}
				}
				?>
				</div>
			</div>
			
			<div id="ajout_grain">
			
				<h4>Ou ajoutez un nouveau grain : </h4>

				<div class="panel panel-default" id="panel-ajout">
					<div class="panel-body">
					    <span id="ajout-grain"></span>
					    <?php
						echo CHtml::htmlButton ( "<span class=\"glyphicon glyphicon-plus\"></span>Ajouter un grain", array (
								'id' => "btn-ajoutgrain",
								'class' => "btn btn-primary"
						));
						?>
						<div id="form_ajout_grain">
							<?php $modelgrain = new Grain;?>
					        <?php echo $form->hiddenField($modelgrain,'idseliste',array("value"=>Yii::app()->user->id)); ?>
					        <?php echo $form->hiddenField($modelgrain,'status',array("value"=>Yii::app()->user->admin_grain ? 1 : 0)); ?>
			                <div class="form-group">
		                        <?php echo $form->label($modelgrain,'idtype',array("class"=>"col-lg-2 control-label")); ?>
		                        <div class="col-lg-10">
	                                <?php 
	                                $data = CHtml::listData(Type::model()->findAll(array('order'=>'lib asc')),
	                                        'id', 
	                                        'lib'
	                                );
	                                echo $form->dropDownList($modelgrain,'idtype',$data,array("class"=>"form-control"));
	                                ?>
		                        </div>
			                </div>
					        
			                <div class="form-group">
		                        <?php echo $form->label($modelgrain,'iddomaine',array("class"=>"col-lg-2 control-label")); ?>
		                        <div class="col-lg-10">
	                                <?php 
	                                $data = CHtml::listData(Domaine::model()->findAll(array('order'=>'lib asc')),
	                                        'id', 
	                                        'lib'
	                                );
	                                echo $form->dropDownList($modelgrain,'iddomaine',$data,array("class"=>"form-control"));
	                                ?>
		                        </div>
			                </div>

					        <div id="form-group-nom" class="form-group<?php if ($modelgrain->getErrors('nom')) echo " has-error";?>">
					                <?php echo $form->labelEx($modelgrain,'nom',array("class"=>"col-lg-2 control-label")); ?>
					                <div class="col-lg-10"><?php echo $form->textField($modelgrain,'nom',array("class"=>"form-control","placeholder"=>"Saisir le nom du grain",'maxlength'=>255)); ?></div>
					        </div>
					        
					        <div class="form-group<?php if ($modelgrain->getErrors('descr')) echo " has-error";?>">
					                <?php echo $form->labelEx($modelgrain,'descr',array("class"=>"col-lg-2 control-label")); ?>
					                <div class="col-lg-10"><?php echo $form->textArea($modelgrain,'descr',array("class"=>"form-control","placeholder"=>"Saisir la description du grain")); ?></div>
					        </div>
					        
					        <div class="form-group<?php if ($modelgrain->getErrors('valeur')) echo " has-error";?>">
					                <?php echo $form->labelEx($modelgrain,'valeur',array("class"=>"col-lg-2 control-label")); ?>
					                <div class="col-lg-10"><?php echo $form->textField($modelgrain,'valeur',array("class"=>"form-control","placeholder"=>"Saisir la valeur du grain",'maxlength'=>255)); ?></div>
					        </div>
					        
					        <div class="form-group<?php if ($modelgrain->getErrors('unite')) echo " has-error";?>">
					                <?php echo $form->labelEx($modelgrain,'unite',array("class"=>"col-lg-2 control-label")); ?>
					                <div class="col-lg-10"><?php echo $form->textField($modelgrain,'unite',array("class"=>"form-control","placeholder"=>"Saisir l'unité du grain",'maxlength'=>1)); ?></div>
					        </div>
					        
					        <div class="form-group">
				                <div class="col-lg-offset-2 col-lg-10">
				                	<?php echo CHtml::Button('Ajouter le grain',array("class"=>"btn btn-primary","id"=>"btn-addgrain"));?> 
				                	<?php echo CHtml::Button('Annuler l\'ajout',array("class"=>"btn btn-sm btn-info","id"=>"btn-annulegrain"));?> 
				                </div>
					        </div>
						</div>
				  	</div>
				</div>
				
			</div>

	</div>
</div>

<div id="demande_etape_deux">

	<div class="form-group<?php if ($model->getErrors('nb')) echo " has-error";?>">
		<?php echo $form->labelEx($model,'nb',array("class"=>"col-lg-3 control-label")); ?>
		<div class="col-lg-9"><?php echo $form->textField($model,'nb',array("class"=>"form-control","placeholder"=>"Saisir le nombre d'unité de la demande")); ?></div>
	</div>

	<div class="form-group<?php if ($model->getErrors('date_souhait')) echo " has-error";?>">
		<?php echo $form->labelEx($model,'date_souhait',array("class"=>"col-lg-3 control-label")); ?>
		<div class="row">
			<div class="col-lg-4"><?php echo $form->textField($model,'date_souhait',array("class"=>"form-control","placeholder"=>"Choisir la date souhaitée")); ?></div>
			<span class="help-block">Format : année-mois-jour ; exemple : 2013-12-28</span>
		</div>
	</div>
	
	<div class="form-group<?php if ($model->getErrors('date_stricte')) echo " has-error";?>">
			<?php echo $form->label($model,'date_stricte',array("class"=>"col-lg-3 control-label")); ?>
			<div class="col-lg-9">
			<div class="btn-group" data-toggle="buttons">
				<label class="btn btn-primary<?php if ($model->date_stricte == 1) echo " active";?>">
					<?php echo $form->radioButton($model,'date_stricte',array("id"=>"optionsDate_stricte1","name"=>"Demande[date_stricte]","uncheckValue"=>NULL,"value"=>1)); ?> Oui
				</label> 
				<label class="btn btn-primary<?php if ($model->date_stricte == 2) echo " active";?>">
					<?php echo $form->radioButton($model,'date_stricte',array("id"=>"optionsDate_stricte2","name"=>"Demande[date_stricte]","uncheckValue"=>NULL,"value"=>2)); ?> Non
				</label> 
			</div>
		</div>
	</div>
	
	<div class="form-group<?php if ($model->getErrors('date_expiration')) echo " has-error";?>">
		<?php echo $form->labelEx($model,'date_expiration',array("class"=>"col-lg-3 control-label")); ?>
		<div class="row">
			<div class="col-lg-4"><?php echo $form->textField($model,'date_expiration',array("class"=>"form-control","placeholder"=>"Choisir la date d'expiration de la demande")); ?></div>
			<span class="help-block">Format : année-mois-jour ; exemple : 2013-12-28</span>
		</div>
	</div>
	
	<div id="form-group-mode" class="form-group<?php if ($model->getErrors('modecontact')) echo " has-error";?>">
		<?php echo $form->labelEx($model,'modecontact',array("class"=>"col-lg-3 control-label")); ?>
		<div class="col-lg-9">
			<div class="btn-group" data-toggle="buttons">
				<label class="btn btn-primary<?php if ($model->modecontact == 0) echo " active";?>">
					<?php echo $form->radioButton($model,'modecontact',array("id"=>"optionsModecontact0","name"=>"Demande[modecontact]","uncheckValue"=>NULL,"value"=>0)); ?> Messagerie interne
				</label>
				<label class="btn btn-primary<?php if ($model->modecontact == 1) echo " active";?>">
					<?php echo $form->radioButton($model,'modecontact',array("id"=>"optionsModecontact1","name"=>"Demande[modecontact]","uncheckValue"=>NULL,"value"=>1)); ?> Email
				</label> 
				<label class="btn btn-primary<?php if ($model->modecontact == 2) echo " active";?>">
					<?php echo $form->radioButton($model,'modecontact',array("id"=>"optionsModecontact2","name"=>"Demande[modecontact]","uncheckValue"=>NULL,"value"=>2)); ?> Téléphone fixe
				</label> 
				<label class="btn btn-primary<?php if ($model->modecontact == 3) echo " active";?>">
					<?php echo $form->radioButton($model,'modecontact',array("id"=>"optionsModecontact3","name"=>"Demande[modecontact]","uncheckValue"=>NULL,"value"=>3)); ?> Téléphone portable
				</label>
			</div>
		</div>
	</div>
	
	<div class="form-group<?php if ($model->getErrors('commentaire')) echo " has-error";?>">
		<?php echo $form->labelEx($model,'commentaire',array("class"=>"col-lg-3 control-label")); ?>
		<div class="col-lg-9"><?php echo $form->textArea($model,'commentaire',array("class"=>"form-control","placeholder"=>"Saisir un commentaire éventuelle sur votre demande")); ?></div>
	</div>
	
	<div class="form-group">
	    <?php echo $form->labelEx($model,'idseliste_cible',array("class"=>"col-lg-3 control-label")); ?>
	    <div class="col-lg-9">
		    <?php 
		    $vide =  array('0'=>'Tous les sélistes');
		    echo $form->dropDownList($model,'idseliste_cible',$vide,array("class"=>"form-control"));
		    ?>
	    </div>
	</div>
    
</div>
	
<div class="form-group">
	<div class="col-lg-offset-3 col-lg-9">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Faire la demande' : 'Modifier la demande',array("class"=>"btn btn-primary","id"=>"btn-validedemande")); ?>
		<?php echo CHtml::link($model->isNewRecord ? "Annuler la demande" : "Annuler les modifications",array('demande/admin'),array('class'=>"btn btn-sm btn-info"));	?>
	</div>
</div>



<?php $this->endWidget(); ?>

</div>
<!-- form -->